package com.gaojiawei.multiThread05_ThreeSpecialAtomic;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 *解决CAS带来的ABA问题
 */
public class l05_ResolveCASABA {
//    private static AtomicReference<String> atomicReference = new AtomicReference<String>("AAA",1);

    private static AtomicStampedReference<String> atomicStampedReference = new AtomicStampedReference<>("aaa",1);


    @Test
    public void testAtomicStampedReference(){
        String oldValue = atomicStampedReference.getReference();
        int oldVersion = atomicStampedReference.getStamp();

        //注意这里好想不能写oldversion++ 会导致版本不变
        atomicStampedReference.compareAndSet("aaa","bbb",oldVersion,oldVersion+1);
        System.out.println(atomicStampedReference.getReference()+atomicStampedReference.getStamp());



    }
}
